import axios from 'axios'
var common = {
  // arr:[{
  //   name:[]
  // },{
  //   name:[]
  // }]
  // 从某个数组内删除某个元素
  spliceArr: async function (arr, data) {
    for (let i = 0; i < arr.length; i++) {
      var name = arr[i].name
      var index = name.findIndex(item => {
        return item === data
      })
      // console.log(arr[0].a)
      name.splice(index, 1)
      await this.getuserImg(name[0]).then(option => {
        arr[i].imgUrl = option
      })
    }
  },
  // 只获取头像
  getuserImg: async function (name, that) {
    const res = await axios.post('/getuserInfo', {
      username: name
    })
    // console.log(this.username + ':' + res)
    if (res.meta.status !== 200) {
      return that.$Message.error('获取头像失败')
    }
    return res.data.imgUrl
  },
  // 获取完整信息
  getuserInfo: async function (name, that) {
    const res = await axios.post('/getuserInfo', {
      username: name
    })
    // console.log(this.username + ':' + res)
    if (res.meta.status !== 200) {
      return that.$Message.error('获取个人信息失败')
    }
    return res.data
  },
  // 获取版块列表
  async getcollege(that) {
    const res = await axios.get('/getcollege')
    if (res.meta.status !== 200) {
      return that.$Message.error('获取部门列表失败')
    }
    return res.data
  },
  // 时间过滤
  timeFilter() {
    var t = new Date()
    var y = t.getFullYear()
    var m = t.getMonth() + 1
    var d = t.getDate()
    var hh = t.getHours()
    var mm = t.getMinutes()
    return `${y}-${m}-${d} ${hh}:${mm}`
  },
  // 根据level获取用户数据
  async getUserList(level) {
    const res = await axios.get('/users/' + level)
    if (res.meta.status !== 200) {
      return this.$Message.error('获取用户列表失败')
    }
    return res.data
  },
  // 格式化用户列表数据
  userListFormat(data) {
    data.forEach(item => {
      item.gender = item.gender === '1' ? '男' : '女'
      item.email = 'email' in item ? item.email : '无'
      item.followLength = item.follow.length
      item.fansLength = item.fans.length
    })
    return data
  },
  // 对象赋值另一个对象部分数据
  async assignmentObject(obj1, obj2) {
    var objName2 = Object.getOwnPropertyNames(obj2)
    objName2.forEach(item => {
      obj2[item] = obj1[item]
    })
    return obj2
  },
  // 比较两个对象属性值的不同并返回不同的属性
  async compareObject(obj1, obj2) {
    var objName2 = Object.getOwnPropertyNames(obj2)
    var newObj = {}
    await objName2.forEach(item => {
      if (obj1[item] !== obj2[item]) {
        newObj[item] = obj2[item]
      }
    })
    return newObj
  },
  // 1男0女的转换
  // dir:1 数字换字
  genderChange(dir, obj1) {
    if (obj1.gender) {
      if (dir) {
        obj1.gender = obj1.gender === '1' ? '男' : '女'
      } else {
        obj1.gender = obj1.gender === '男' ? '1' : '0'
      }
    }
  },
  // 删除
  async deletes(scope, url, that) {
    // 弹出确定取消框，是否删除用户
    const confirmResult = await that.$confirm(
      '请问是否要永久删除',
      '删除提示',
      {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }
    ).catch(err => err)
    // 如果用户点击确认，则confirmResult 为'confirm'
    // 如果用户点击取消, 则confirmResult获取的就是catch的错误消息'cancel'
    if (confirmResult !== 'confirm') {
      return 0
    }
    // 发送请求根据id完成删除操作
    const res = await axios.delete(url + scope)
    if (res.meta.status !== 200) return that.$Message.error('删除失败')
    that.$Message.success('删除成功')
    return 1
  },
  // 删除某条数据
  ArrSplice: function (data, keys, scope) {
    var index = data.findIndex(item => {
      return item[keys] === scope
    })
    data.splice(index, 1)
  },
  // 积分制规则
  // 每日登录+1
  // 发帖+1
  // 评论+1
  // 点赞+1
  async editInteg(userName, types, number, pub, noticeMsg, id) {
    var notices
    switch (types) {
      case 'integ.login': case 'integ.post':
        notices = {
          date: this.timeFilter()
        }
        break
      default:
        notices = {
          publisher: pub,
          msg: noticeMsg,
          date: this.timeFilter(),
          isread: 0,
          postId: id
        }
        break
    }
    const res = await axios.post('/editInteg', {
      name: userName,
      type: types,
      notice: notices,
      integ: number
    })
    if (res.meta.status !== 200) {
      return 0
    }
    return 1
  },
  // 计算积分总和
  integSum(integ) {
    var integName = Object.getOwnPropertyNames(integ)
    var sum = 0
    integName.forEach(item => {
      sum += parseInt(integ[item])
    })
    return sum
  },
  // 管理修改积分
  async editinteg(username, data, that) {
    const res = await axios.post('/editnoticeInfo', {
      username: username,
      editData: data
    })
    if (res.meta.status !== 200) {
      that.$Message.error('服务器发生错误，请重试')
    }
    that.$Message.success('修改成功')
  },
  // 根据姓名查找通知
  async getnotice(name, that) {
    const res = await axios.get('/noticeInfo/' + name)
    if (res.meta.status !== 200) {
      return that.$Message.error('获取积分信息失败')
    }
    return res.data
  },
  // 将通知内的未读改为已读
  async noticeRead(username, _id, factors, types, number, that) {
    const res = await axios.post('/noticeRead', {
      name: username,
      id: _id,
      factor: factors,
      type: types,
      num: number
    })
    if (res.meta.status !== 200) {
      return that.$Message.error('服务器出错')
    }
  }
}
export default common
